#include "widget.h"
#include "ui_widget.h"

#include <QApplication>
#include <QDir>
#include <QPluginLoader>
#include <QRegExp>
#include <QRegExpValidator>
#include <QIntValidator>
#include <QDoubleValidator>

class WidgetPrivate
{
    WidgetPrivate(Widget *q)
        : q_ptr(q)
        , m_xllSettings(nullptr)
        , m_myepInter(new ComDeepinDbusMyepInterface("com.deepin.dbus.myep", "/com/deepin/dbus/myep", QDBusConnection::sessionBus(), q_ptr))
        , m_pictureBoxInterface(nullptr)
    {
    }

    ~WidgetPrivate(){}

    Q_DECLARE_PUBLIC(Widget)
    Widget *q_ptr;

    QGSettings *m_xllSettings;
    ComDeepinDbusMyepInterface *m_myepInter;
    PictureBoxInterface *m_pictureBoxInterface;
};

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , d_ptr(new WidgetPrivate(this))
{
    ui->setupUi(this);
    this->setWindowTitle("防疫工作检查客户端");
    ui->label_text->setAlignment(Qt::AlignCenter);

    //年龄输入限制为：0-120
    ui->lineEdit_setAge->setValidator(new QIntValidator(0, 120, ui->lineEdit_setAge));

    //身份证如输入限制为：0-9 a-z A-Z
    QRegExp idRegExp{"^[0-9a-zA-Z]{0,}$"};
    ui->lineEdit_setId->setValidator(new QRegExpValidator(idRegExp, ui->lineEdit_setId));

    //温度限制为：0-100 保留一位小数
    QRegExp temperatureRegExp("100|([0-9]{0,2}[\.][0-9])");
    ui->lineEdit_setTemperature->setValidator(new QRegExpValidator(temperatureRegExp, ui->lineEdit_setTemperature));

    loadPlugin();

    Q_D(Widget);
    if (QGSettings::isSchemaInstalled("com.deepin.personnel.info"))
    {
        qDebug() << "com.deepin.personnel.info installed.";
        d->m_xllSettings = new QGSettings("com.deepin.personnel.info", "/com/deepin/personnel/info/");
        connect(d->m_xllSettings, &QGSettings::changed, this, &Widget::slotUpdateInfo);

        QStringList keyList = d->m_xllSettings->keys();
        for (auto key: keyList)
            slotUpdateInfo(key);
    }
    else
        qDebug() << "com.deepin.personnel.info is not installed.";

    connect(d->m_myepInter, &ComDeepinDbusMyepInterface::signalTemperatureIsOk, this, [=](bool isOk){
        if (isOk)
        {
            ui->label_text->setStyleSheet("color:green;font:30pt;");
            ui->label_text->setText(QStringLiteral("放行"));
        }
        else
        {
            ui->label_text->setStyleSheet("color:red;font:30pt;");
            ui->label_text->setText(QStringLiteral("隔离"));
        }
    });
}

Widget::~Widget()
{
    delete ui;
}

bool Widget::loadPlugin()
{
    Q_D(Widget);

    bool ret = false;

    //获取当前应用程序所在目录
    QDir plugInDir(qApp->applicationDirPath());
    plugInDir.cd("../plugins");

    //遍历目录下的所有插件
    for (auto filename : plugInDir.entryList(QDir::Files))
    {
        //加载插件
        QPluginLoader pluginLoader(plugInDir.absoluteFilePath(filename));
        QObject *plugin = pluginLoader.instance();
        if (plugin)
        {
            QString pluginClass = plugin->metaObject()->className();
            if (pluginClass == "PictureBoxPlugin")
            {
                d->m_pictureBoxInterface = qobject_cast<PictureBoxInterface *>(plugin);
                if (d->m_pictureBoxInterface)
                {
                    QWidget *pPictureWidget = d->m_pictureBoxInterface->createPictureBox(this);
                    ui->gridLayout_picturebox->addWidget(pPictureWidget);
                    ret = true;
                }
                else
                {
                    QLabel *label = new QLabel("插件未加载");
                    label->setAlignment(Qt::AlignCenter);
                    ui->gridLayout_picturebox->addWidget(label);
                }
                break;
            }
        }
    }
    return ret;
}

void Widget::on_lineEdit_setName_textChanged(const QString &arg1)
{
    Q_D(Widget);
    if (d->m_xllSettings)
    {
        d->m_xllSettings->trySet("name",arg1);
    }
}

void Widget::on_lineEdit_setAge_textChanged(const QString &arg1)
{
    Q_D(Widget);
    if (d->m_xllSettings)
    {
        d->m_xllSettings->trySet("age",arg1);
    }
}

void Widget::on_lineEdit_setId_textChanged(const QString &arg1)
{
    Q_D(Widget);
    if (d->m_xllSettings)
    {
        d->m_xllSettings->trySet("id",arg1);
    }
}

void Widget::on_lineEdit_setAddress_textChanged(const QString &arg1)
{
    Q_D(Widget);
    if (d->m_xllSettings)
    {
        d->m_xllSettings->trySet("address",arg1);
    }
}

void Widget::on_lineEdit_setTemperature_textChanged(const QString &arg1)
{
    QString str = arg1;
}

void Widget::on_pushButton_ok_clicked()
{
    QString strName = ui->lineEdit_setName->text();
    QString strAge = ui->lineEdit_setAge->text();
    QString strId = ui->lineEdit_setId->text();
    QString strAddress = ui->lineEdit_setAddress->text();
    QString strTemperature = ui->lineEdit_setTemperature->text();

    if (strTemperature.isEmpty())
    {
        QMessageBox::information(this, QStringLiteral("提示"), QStringLiteral("请填写温度信息！"));
    }
    else
    {
        ui->label_text->setText("");
        ui->thermometerWidget->setValueWithAnimation(strTemperature.toDouble());
        Q_D(Widget);
        d->m_myepInter->checkTemperature(strName, strAge.toInt(), strId, strAddress, strTemperature.toDouble());
    }
}

void Widget::slotUpdateInfo(const QString &key)
{
    Q_D(Widget);
    if (key == "name")
    {
        QString strName = d->m_xllSettings->get("name").toString();
        if (strName.isEmpty())
            strName = "-";
        ui->label_getName->setText(strName);
    }
    else if (key == "age")
    {
        QString strAge = d->m_xllSettings->get("age").toString();
        ui->label_getAge->setText(strAge);
    }
    else if (key == "id")
    {
        QString strId = d->m_xllSettings->get("id").toString();
        if (strId.isEmpty())
            strId = "-";
        ui->label_getId->setText(strId);
    }
    else if (key == "address")
    {
        QString strAddress = d->m_xllSettings->get("address").toString();
        if (strAddress.isEmpty())
            strAddress = "-";
        ui->label_getAddress->setText(strAddress);
    }
}
